//
//  AddSettingCell.m
//  WeChatRedbag
//  Created by 卖女孩的小火柴 on 2021/12/17.
#import <UIKit/UIKit.h>
#import "MRedBagHead.h"

@interface WCTableViewManager
-(long long)numberOfSectionsInTableView:(UITableView*)tableview;
@end

@interface NewSettingViewController:UIViewController

@end

%hook WCTableViewManager
%new
-(void)switchchange:(UISwitch*)switchview{
    [GYUserDefault setBool:switchview.isOn forKey:GYSWITCHKEY];
    [GYUserDefault synchronize];
    [MSHookIvar<UITableView*>(self, "_tableView") reloadData];
}
%new
-(void)textfieldchange:(NSNotification*)notication{
     UITextField* textfield=[notication object];
    [GYUserDefault setValue:textfield.text forKey:GYTIMEKEY];
    [GYUserDefault synchronize];
}
- (void)scrollViewWillBeginDragging:(id)arg1{
    %orig;
    
    if([MSHookIvar<UITableView*>(self, "_tableView").nextResponder.nextResponder isKindOfClass:%c(NewSettingViewController)]){
        [MSHookIvar<UITableView*>(self, "_tableView") endEditing:YES];
    }
}
//
-(id)tableView:(UITableView*)tableview cellForRowAtIndexPath:(NSIndexPath*)indextpath{
    if([tableview.nextResponder.nextResponder isKindOfClass:%c(NewSettingViewController)]&&indextpath.section==[self numberOfSectionsInTableView:tableview]-1){
        
        UITableViewCell* cell=nil;
        if(indextpath.row==0){
            static NSString* switchcell=@"switchcell";
            cell=[tableview dequeueReusableCellWithIdentifier:switchcell];
            if(!cell){
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            }
            
            cell.textLabel.text=@"自动抢群红包";
            UISwitch* switchbtn=[[UISwitch alloc]init];
            switchbtn.on=[GYUserDefault boolForKey:GYSWITCHKEY];
            [switchbtn addTarget:self action:@selector(switchchange:) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView=switchbtn;
            
            cell.imageView.image=[UIImage imageNamed:[GYUserDefault boolForKey:GYSWITCHKEY]==1?@"unlocked":@"locked"];
            
        }else{
            static NSString* timecell=@"timecell";
            cell=[tableview dequeueReusableCellWithIdentifier:timecell];
            if(!cell){
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            }
            cell.textLabel.text=@"等待时间(秒)";
            UITextField* timefield=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 120, 40)];
            timefield.keyboardType=UIKeyboardTypeDecimalPad;
            timefield.borderStyle=UITextBorderStyleRoundedRect;
            timefield.text=[GYUserDefault valueForKey:GYTIMEKEY];
            cell.accessoryView=timefield;
            cell.imageView.image=[UIImage imageNamed:@"clock"];
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldchange:) name:UITextFieldTextDidChangeNotification object:timefield];
            
        }
        cell.backgroundColor=[UIColor whiteColor];
        return cell;
    }else{
      return %orig;
    }
}

-(long long)tableView:(UITableView*)tableview numberOfRowsInSection:(long long)section{
    if([tableview.nextResponder.nextResponder isKindOfClass:%c(NewSettingViewController)]&&section==[self numberOfSectionsInTableView:tableview]-1){
       return 2;
    }else{
       return %orig;
    }

}
-(long long)numberOfSectionsInTableView:(UITableView*)tableview{
    if([tableview.nextResponder.nextResponder isKindOfClass:%c(NewSettingViewController)]){
       return %orig+1;
    }else{
      return  %orig;
    }
}

-(double)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
    if([tableView.nextResponder.nextResponder isKindOfClass:%c(NewSettingViewController)]){
      return  44;
    }else{
       return %orig;
    }
}
%end


%hook NewSettingViewController
- (void)viewDidLoad{
    %orig;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
}
%new
-(void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    int height = keyboardRect.size.height;
    self.view.frame=CGRectMake(0,-height,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    
}
%new
-(void)keyboardWillHide:(NSNotification *)notification{
    self.view.frame=CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
%end

